package proyectoFC_2_3_3.koyicauto.ImageGallery;

import java.util.ArrayList;
import java.util.List;

import proyectoFC_2_3_3.koyicabuto.GrupoActivity;
import proyectoFC_2_3_3.koyicabuto.R;
import android.app.Activity;
import android.content.Context;
import android.content.Intent;
import android.database.Cursor;
import android.graphics.Bitmap;
import android.os.Bundle;
import android.provider.MediaStore;
import android.view.LayoutInflater;
import android.view.View;
import android.view.View.OnClickListener;
import android.view.ViewGroup;
import android.widget.BaseAdapter;
import android.widget.GridView;
import android.widget.ImageView;

public class ImageGalleryActivity extends Activity{
	
	//Variables para la galleria
	 private int count;
     private List<Object[]> thumbnails = new ArrayList<Object[]>();
     private ImageAdapter imageAdapter;
     
     private Long idGrupoSeleccionado = null;

	
	@Override
    public void onCreate(Bundle savedInstanceState) {
        super.onCreate(savedInstanceState);
        setContentView(R.layout.imagegallery);
        
        Bundle extras = getIntent().getExtras();
        if(extras != null && extras.containsKey("idGrupoSeleccionado")){
        	idGrupoSeleccionado = extras.getLong("idGrupoSeleccionado");
		}
        
        final String[] columns = { MediaStore.Images.Media.DATA, MediaStore.Images.Media._ID };
        final String orderBy = MediaStore.Images.Media._ID;
        Cursor imagecursor = managedQuery(MediaStore.Images.Media.EXTERNAL_CONTENT_URI, columns, null, null, orderBy);
        int image_column_index = imagecursor.getColumnIndex(MediaStore.Images.Media._ID);
        this.count = imagecursor.getCount();
        for (int i = 0; i < this.count; i++) {
        	Object[] array = new Object[2];
            imagecursor.moveToPosition(i);
            int id = imagecursor.getInt(image_column_index);
            //Bitmap de la foto
            array[0] = MediaStore.Images.Thumbnails.getThumbnail(getApplicationContext().getContentResolver(), id, 
					   MediaStore.Images.Thumbnails.MICRO_KIND, null);
            //Id de la Foto
            array[1] = imagecursor.getInt(1);
            
            thumbnails.add(array);
        }
        GridView imagegrid = (GridView) findViewById(R.id.ImageGrid);
        imageAdapter = new ImageAdapter();
        imagegrid.setAdapter(imageAdapter);
        imagecursor.close();
    }
	
	public class ImageAdapter extends BaseAdapter {
		 
       private LayoutInflater mInflater;
       
       public ImageAdapter() {
           mInflater = (LayoutInflater) getSystemService(Context.LAYOUT_INFLATER_SERVICE);
       }
       public int getCount() {
           return count;
       }
		  
       public Object getItem(int position) {
           return position;
       }
 
       public long getItemId(int position) {
           return position;
       }
		  
		public View getView(int position, View convertView, ViewGroup parent) {
			final ViewHolder holder;
            if (convertView == null) {
           	 holder = new ViewHolder();
                convertView = mInflater.inflate(R.layout.photogalleryitem, null);
                holder.imageview = (ImageView) convertView.findViewById(R.id.thumbImage);
 
                convertView.setTag(holder);
            }else {
                holder = (ViewHolder) convertView.getTag();
            }
            
            holder.id = position;
            
            holder.imageview.setId(position);
            final Object[] array = thumbnails.get(position);
            holder.imageview.setImageBitmap((Bitmap)array[0]);
            holder.imageview.setOnClickListener(new OnClickListener() {
				
				public void onClick(View v) {
					onCLickFotoID((Integer)array[1]);
				}
			});
            
            return convertView;
		}
    }
	 
	 private class ViewHolder {
        ImageView imageview;
        int id;
    }

	private void onCLickFotoID(int id){
		Intent i = new Intent(this, GrupoActivity.class);
		i.putExtra("idFotoPerfil", id);
		i.putExtra("tabSeleccionada", "tabDatosUsuario");
		i.putExtra("idGrupoSeleccionado", idGrupoSeleccionado);
		startActivity(i);
	}
}
